package com.as.server.service.Impl;

import com.as.server.entity.Follow;
import com.as.server.mapper.FollowMapper;
import com.as.server.service.FollowService;
import com.github.pagehelper.Page;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class FollowServiceImpl implements FollowService {
    @Autowired
    FollowMapper mapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Cacheable(cacheNames = "FollowSpace")
    public Page<Follow> selectAllByUidtoPage(Long uidTo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return mapper.selectAllByUidtoPage(uidTo);
    }

    @Override
    @Cacheable(cacheNames = "FollowSpaceFans")
    public Page<Follow> selectAllByUidFollowPage(Long uidFollow, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return mapper.selectAllByUidFollowPage(uidFollow);
    }

    @Override
    @Cacheable(cacheNames = "FollowSpaceCount")
    public List selectNumber(Long uid) {
        int concernNumber = mapper.selectByConcernNumber(uid);
        int fanNumber = mapper.selectByFanNumber(uid);
        List num = new ArrayList<>();
        num.add(concernNumber);
        num.add(fanNumber);
        return num;
    }

    /**
     * @CacheEvict 清空redis中叫followSpace：下的数据
     * @param record 关注信息
     * @return
     */
    @Override
    public int insert(Follow record) {
        //根据uidto 进行模糊查询把相关分页缓存清掉
        Set<String> keys = redisTemplate.keys("FollowSpace*"+record.getUidto()+"*");
        redisTemplate.delete(keys);
        return mapper.insert(record);
    }
}
